#include "cstringutils.h"

/**
 * toLower - converts upper-case letters in string into lower-case
 * @param str
 * @return lower-case string
 */
string CStringUtils::toLower(string str) {
  for (unsigned int i = 0; i < str.length(); i++) str[i] = tolower(str[i]);
  return str;
}
//---------------------------------------------------------------------------

/**
 * toUpper - converts lower-case letters in string into upper-case
 * @param str
 * @return upper-case string
 */
string CStringUtils::toUpper(string str) {
  for (unsigned int i = 0; i < str.length(); i++) str[i] = toupper(str[i]);
  return str;
}
//---------------------------------------------------------------------------

/**
 * replaceAll - replaces all occurences of substring by another
 * @param origSubStr
 * @param newSubStr
 * @param str
 * @return modified string
 */
string CStringUtils::replaceAll(string origSubStr, string newSubStr,
                                string str) {
  unsigned int pos;
  while ((pos = str.find(origSubStr)) != string::npos) {
    str.replace(pos, origSubStr.length(), newSubStr);
  }
  return str;
}
//---------------------------------------------------------------------------

/**
 * removeSpaces - removes spaces from string
 * @param str
 * @return string without spaces
 */
string CStringUtils::removeSpaces(string str) {
  return replaceAll(" ","", str);
}
//---------------------------------------------------------------------------

/**
 * explode - splits string by delimiter substring
 * @param delim
 * @param str
 * @return list of substrings
 */
vector<string> CStringUtils::explode(string delim, string str) {

  vector<string> result;

  unsigned int pos;
  while ((pos = str.find(delim)) != string::npos) {
    result.push_back(str.substr(0, pos));
    str = str.substr(pos + delim.length(), str.size());
  }
  result.push_back(str);
  return result;
}
//---------------------------------------------------------------------------

/**
 * getDirFromFilePath - retrieves directory from path to file
 * @param str
 * @return directory of the file
 */
string CStringUtils::getDirFromFilePath(string str) {
  
  string file = str;
  if (file.find("/", 0) == string::npos && file.find("\\", 0) == string::npos) {
    return string("");
  }  
  int pos1 = str.find_last_of('\\', str.length());
  int pos2 = str.find_last_of('/', str.length());
  
  if (pos1 > pos2) file = str.substr(0, pos1 + 1); 
  else if (pos1 < pos2) file = str.substr(0, pos2 + 1);  
  
  return file;
}
//---------------------------------------------------------------------------

/**
 * getFileNameFromFilePath - retrieves filename from filepath
 * @param str
 * @return filename
 */
string CStringUtils::getFileNameFromFilePath(string str) {
  
  string file = str;
  int pos1 = str.find_last_of('\\', str.length());
  int pos2 = str.find_last_of('/', str.length());
  
  if (pos1 > pos2) file = str.substr(pos1 + 1, str.length()); 
  else if (pos1 < pos2) file = str.substr(pos2 + 1, str.length());  
  
  return file;
}
//---------------------------------------------------------------------------

/**
 * removeMultipleSubstr - removes multiple occurences of substring
 * @param str
 * @param sub
 * @return string with removed mupltiple substring occurences
 */
string CStringUtils::removeMultipleSubstr(string str, string sub) {
  return replaceAll(sub + sub, sub, str);
}
//---------------------------------------------------------------------------
